<script setup>
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import {selectStudentHomeWorkService,
    handleStudentHomeWorkService
} from '@/api/StudentHomeWork.js'
const total = ref(0)
const params = ref({
  courseName:'',
  userName:'',
  pageNum:1,
  pageSize:5
})
const paramsDown = ref({
    fileName:'',
    path:''
})
const handleClick = async (row) => {
    paramsDown.value.fileName = row.homeWorkName
    paramsDown.value.path = row.homeWorkPath
    location.href="http://localhost:8080/api/file/down?fileName="+row.homeWorkName+"&path="+encodeURIComponent(row.homeWorkPath)
}

const homeWorkRef = ref(null)
const studentHomeWorkVo = ref({
  id:'',
  courseName:'',
  courseSubName: '',
  userName: '',
  homeWorkName: '',
  homeWorkPath: '',
  homeWorkPoint: '',
  status:'',
  statusStr:''
})

const point = ref()

const studentHomeWorkList = ref([])

const handleCurrentChange = (page) => {
    params.value.pageNum = page
    getstudentHomeWorkList()
}
const getstudentHomeWorkList = async ()=> {
    const res = await selectStudentHomeWorkService(params.value)
    studentHomeWorkList.value = res.data.data
    total.value = res.data.total
}
getstudentHomeWorkList()


const paramsPoint = ref({
    id:'',
    homeWorkPoint:''
})
const dialogVisible = ref(false)
const handlePoint = (row) => {
    dialogVisible.value = !dialogVisible.value
    paramsPoint.value.id = row.id
}
const confirmPoint = async () => {
    paramsPoint.value.homeWorkPoint = point.value
    await handleStudentHomeWorkService(paramsPoint.value)
    ElMessage.success('成功！')
    getstudentHomeWorkList()
    paramsPoint.value = {}
    point.value = ''
    dialogVisible.value = !dialogVisible.value
}

const search = () => {
  getstudentHomeWorkList()
}
</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
          <div style="display: flex;justify-content: space-between;">
            <div class="search">
                <div>
                    课程：
                    <el-input v-model="params.courseName" style="width: 240px" placeholder="请输入课程名称" />
                </div>
                <div style="margin-left: 20px;">
                    学生姓名：
                    <el-input v-model="params.userName" style="width: 240px" placeholder="请输入姓名" />
                </div>
                <div style="margin-left: 20px;">
                    <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                </div>
            </div>
          </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="studentHomeWorkList" style="width: 100% ;" height="300" >
                <el-table-column prop="courseName" label="课程名称" width="120" />
                <el-table-column prop="courseSubName" label="课节名称" width="120" />
                <el-table-column prop="userName" label="学生名称" width="120" />
                <el-table-column prop="homeWorkName" label="作业名称" width="120" />
                <el-table-column prop="homeWorkPoint" label="分数" width="120" />
                <el-table-column prop="statusStr" label="处理状态" width="600" />
                <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="{row}">
                    <el-button link type="primary" size="small" @click="handleClick(row)">
                        作业下载
                    </el-button>
                    <el-button link type="primary" size="small" @click="handlePoint(row)">评分</el-button>
                </template>
                </el-table-column>
            </el-table>
            <div style="margin-top: 20px;">
              <el-pagination
              v-model:current-page="params.pageNum"
              v-model:page-size="params.pageSize"
              :background="true"
              layout="jumper,prev, pager, next,total"
              :total=total
              @current-change="handleCurrentChange"
              style="justify-content: end;"
            /></div>
        </el-card>
    </div>
  </div>

  <el-dialog
    v-model="dialogVisible"
    title="评分"
    width="500"
    :before-close="handleClose"
  >
    学生分数：<el-input v-model="point" style="width: 240px" placeholder="请输入分数" />
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmPoint">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>